import request from "../../utils/request"
import {
  HttpMethod
} from "../../utils/request"

/**
 * 创建订单
 * @param {[]} orders 订单列表
 * @returns 返回创建订单结果
 * @throws 参数传递错误
 */
export function createOrders(orders) {
  if (!Array.isArray(orders)) {
    throw "参数 data 一定是一个数组!"
  }
  return request({
    url: "/canteen/order",
    data: orders,
    method: HttpMethod.POST
  })
}

/**
 * 根据订单号查询订单
 * @param {string} orderNo 订单号
 */
export function getOrderByNo(orderNo) {
  return request({
    url: "/canteen/order/list/" + orderNo,
    data: {},
    method: HttpMethod.GET
  })
}

/**
 * 添加购物车
 * @param {object} food 菜品
 * @returns 返回创建订单结果
 * @throws 参数传递错误
 */
export function addCartApi(food) {
  return request({
    url: "/canteen/cart",
    data: food,
    method: HttpMethod.POST
  })
}

/**
 * 修改购物车, 选择的数量
 * @param {object} food 菜品
 * @returns 返回创建订单结果
 * @throws 参数传递错误
 */
export function updateCartApi(food) {
  return request({
    url: "/canteen/cart",
    data: food,
    method: HttpMethod.PUT
  })
}

/**
 * 删除购物车选择的菜品
 * @param {string} ids 要删除的菜品id
 * @returns 返回创建订单结果
 * @throws 参数传递错误
 */
export function deleteCartApi(ids) {
  let url = "/canteen/cart"
  if (ids) {
    url += "/" + ids
  }
  return request({
    url: url,
    method: HttpMethod.DELETE
  })
}

// 查询购物车列表
export function listCart(query) {
  return request({
    url: '/canteen/cart/list',
    method: HttpMethod.GET,
    data: query
  })
}